import 'package:easy_refresh/easy_refresh.dart';
import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:taonile/config/config.dart';
import 'package:taonile/entity/announcement_list_entity.dart';
import 'package:taonile/routers/routers.dart';

import '../http/api.dart';
import '../http/web_path.dart';
import '../views/default_views.dart';

class Announcement extends StatefulWidget {
  const Announcement({Key? key}) : super(key: key);

  @override
  State<Announcement> createState() => _AnnouncementState();
}

class _AnnouncementState extends State<Announcement> {
  int page = 1;
  List<AnnouncementListDataData> datas = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getDatas();
  }

  getDatas() async {
    dynamic result = await Api.announcementList({'page': page, 'limit': '20'});
    AnnouncementListEntity entity = AnnouncementListEntity.fromJson(result);
    setState(() {
      if (page == 1) {
        datas.clear();
      }
      if (entity.code == 1) {
        page++;
        datas.addAll(entity.data.data);
      }
    });
  }

  Widget getListItem(int index) {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 25.w, vertical: 10.w),
      padding: EdgeInsets.symmetric(vertical: 20.w, horizontal: 20.w),
      decoration: BoxDecoration(
        color: Config.colorWhite,
        borderRadius: BorderRadius.circular(12.w),
      ),
      child: InkWell(
        onTap: () {
          Routes.navigateTo(context, Routes.webView, params: {
            'pageTitle': datas[index].title,
            'webUrl': '${Config.webUrl}${WebPath.announcementDetal}${datas[index].id}'
          });
        },
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              datas[index].title,
              style: TextStyle(
                fontSize: 32.w,
                overflow: TextOverflow.ellipsis,
                color: Config.colorText1,
              ),
            ),
            Padding(padding: EdgeInsets.only(top: 10.w)),
            // Text(
            //   datas[index].content,
            //   maxLines: 15,
            //   style: TextStyle(
            //     fontSize: 24.w,
            //     color: Config.colorText1,
            //   ),
            // ),
            Padding(padding: EdgeInsets.only(top: 15.w)),
            Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                Text(
                  datas[index].createdAt,
                  maxLines: 15,
                  style: TextStyle(
                    fontSize: 26.w,
                    color: Config.colorText3,
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Config.colorBg2,
      appBar: AppBar(
        title: const Text('公告'),
        centerTitle: true,
        backgroundColor: Config.colorBg,
      ),
      body: Container(
          width: double.infinity,
          height: double.infinity,
          child: datas.isNotEmpty
              ? EasyRefresh(
                  header: const MaterialHeader(),
                  footer: const MaterialFooter(),
                  onRefresh: () async {
                    await Future.delayed(const Duration(milliseconds: 200), () {
                      page = 1;
                      getDatas();
                    });
                  },
                  onLoad: () async {
                    getDatas();
                  },
                  child: ListView.builder(
                    // physics: const NeverScrollableScrollPhysics(),
                    itemCount: datas.length,
                    shrinkWrap: true,
                    padding: EdgeInsets.symmetric(vertical: 5.w, horizontal: 1.w),
                    itemBuilder: (BuildContext context, int index) {
                      return getListItem(index);
                    },
                  ),
                )
              : showNullData()),
    );
  }
}
